package com.example.sudoku;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RankActivity extends AppCompatActivity {

    // 定义跳转页面的函数，无参数
    public void RankToMain() {
        // 创建新的Intent
        Intent intent = new Intent();
        // 设置跳转的源Activity和目的Activity
        intent.setClass(RankActivity.this, MainActivity.class);
        // 开始Intent，即开始跳转
        RankActivity.this.startActivity(intent);
        // 关闭RankActivity
        RankActivity.this.finish();
    }

    // 重写OnCreate函数
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 绘制排行榜界面
        setContentView(R.layout.rank);

        // 获取排行榜内容
        // 设置正则表达式
        Pattern pattern = Pattern.compile("[^0-9]");
        Matcher matcher;
        long time;
        // 设置输出字符串
        String [] mode_out = {"简单", "普通", "困难", "极难"};
        String [] flag_out = {"关闭", "开启"};
        String [] dataString = save.return_paihangbang();
        for(int i = 0; i < 10; i++) {
            if(dataString[i].equals("")) break;
            // 匹配
            matcher = pattern.matcher(dataString[i]);
            // 将非数字替换成空格，删除多余空格，然后以空格为分隔符分割
            String string = matcher.replaceAll(" ").trim().replaceAll("[ ]+", " ");
            String [] str = string.split(" ");
            // 构建输出排行榜字符串
            dataString[i] = "分数: " + str[2] + "\t\t\t\t\t\t用时: ";
            time = Long.parseLong(str[0]);
            // 换算成小时:分钟:秒
            int s = (int) (time % 60);
            int m = (int) (time / 60) % 60;
            int h = (int) (time / 3600) % 60;
            // 格式化输出时间字符串
            dataString[i] += String.format(Locale.getDefault(), "%02d:%02d:%02d", h, m, s);
            dataString[i] += "\n难度: "+mode_out[Integer.parseInt(str[1])]
                    +"\t\t\t辅助: "+flag_out[Integer.parseInt(str[3])];
        }

        // 创建数组适配器，参数：Activity，TextView，Array
        ArrayAdapter <String> adapter = new ArrayAdapter <String> (
                RankActivity.this, R.layout.array_adapter, dataString);

        // 获取对应ListView
        ListView listView = findViewById(R.id.list_view);
        listView.setAdapter(adapter);

        // 获取对应按钮
        Button exit_rank = this.findViewById(R.id.exit_rank);
        Button clear_rank = this.findViewById(R.id.clear_rank);

        // 监听：退出按钮
        exit_rank.setOnClickListener((View view) -> {
            // 跳转到开始界面
            RankToMain();
        });

        // 监听：清除记录按钮
        clear_rank.setOnClickListener((View view) -> {
            // 清除排行榜记录
            save.clearFile();
            // 刷新RankActivity
            Intent intent = new Intent(RankActivity.this, RankActivity.class);
            startActivity(intent);
            RankActivity.this.finish();
        });
    }
}

